#include "widget.h"
#include "./ui_widget.h"
#include "PolygonWidget.h"
#include <QDebug>
#include <QThreadPool>
#include <QThread>
#include "threadtraverse.h"
#include <QRunnable>
#include <QScrollBar>
#include "bfsclass.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    setdata();
     ui->widget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
     ui->widget->setStyleSheet("border: 2px solid black;");
     QVBoxLayout *layout = new QVBoxLayout(ui->widget);
     show();
     auto size=ui->widget->geometry();
     qDebug()<<"size："<<size;
     widget=new PolygonWidget(ui->widget);
     ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
     QPushButton *w=new QPushButton("wwowdaw",ui->widget);
     layout->addWidget(w);
     layout->addWidget(widget);
     widget->show();
     layout->setContentsMargins(0, 0, 0, 0);
     layout->setSpacing(0);
     widget->resize(ui->widget->geometry().size());
     widget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
     widget->setPolygon(2, 220, size.width()/2,size.height()/2); // Example: 7-sided polygon with radius 100 centered at (150, 150)
     widget->SetALGraph(alg);
     widget->setStyleSheet("background-color: red;"); // 设置背景颜色为红色
     ui->widget->setLayout(layout);
     //widget->resize(size.width(),size.height());
     //tt= new ThreadTraverse(alg,widget,this);
    // connect(tt,&ThreadTraverse::adddata,this,[this](int data){
    //      this->ui->listWidget->addItem(QString::number(data));
    //  });

     myupdate();
}
void Widget::resizeEvent(QResizeEvent *event) {
    QWidget::resizeEvent(event);
    //widget->resize(1000,1000);
    qDebug()<<"123";
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    alg->InsertVex(ui->lineEdit->text().toInt());
    //widget->SetALGraph(alg);
    //widget->update();
    myupdate();
}


void Widget::on_comboBox_currentIndexChanged(int index)
{
    ui->comboBox_2->clear();
    QStringList qsl;
    for(int i=0;i<alg->vexnum;i++){
        if(QString::number(alg->vertices[i].data)!=ui->comboBox->currentText()){
            qsl.append(QString::number(alg->vertices[i].data));
        }
    }
    ui->comboBox_2->addItems(qsl);
    //widget->update();
}
void Widget::myupdate(){
    ui->comboBox->clear();
    //ui->comboBox_2->clear();
    QStringList qsl;
    for(int i=0;i<alg->vexnum;i++){
        qsl.append(QString::number(alg->vertices[i].data));
    }


    ui->comboBox->addItems(qsl);
    widget->update();
}

void Widget::on_pushButton_2_clicked()
{
    alg->AddArc(ui->comboBox->currentText().toInt(),ui->comboBox_2->currentText().toInt());
    myupdate();
}


void Widget::on_pushButton_3_clicked()
{
    alg->DeleteArc(ui->comboBox->currentText().toInt(),ui->comboBox_2->currentText().toInt());
    myupdate();
}


void Widget::on_pushButton_4_clicked()
{
    QThreadPool::globalInstance()->clear();
    QThreadPool::globalInstance()->start(new ThreadTraverse(alg,widget,this));
}
void Widget::setdata(){
    alg=new ALGraph<int>(DN);
    alg->InsertVex(4);
    alg->InsertVex(9);
    alg->InsertVex(88);
    alg->InsertVex(26);
    alg->InsertVex(59);
    alg->InsertVex(12);

    alg->AddArc(4,88);
    alg->AddArc(9,59);
    alg->AddArc(26,9);

}

void Widget::on_pushButton_5_clicked()
{
    QThreadPool::globalInstance()->clear();
    // alg->BFSTraverse([](ALGraph<int>::VNode *node){
    //     qDebug()<<node->data;
    // });
    QThreadPool::globalInstance()->start(new BFSClass(alg,widget,this));
}

